<template>
    <div v-if="isGraduate">
        <table width="100%" class="personal" style="margin:15px 0">
            <tbody>
                <!-- <tr>
					<td :colspan="secondLabels.length+1" style="font-size:20px;height:40px">学分完成情况</td>
				</tr> -->
                <tr>
                    <td rowspan="2">{{ $t('culturePlan.scoreStatistics.courseType') }}</td>
                    <td
                        v-for="(label,index) in parentData"
                        :key="index"
                        :colspan="label.colspan"
                        :rowspan="label.rowspan"
                    >{{ language == 'en' ? label.labelNameEn : label.labelName }}</td>
                </tr>
                <tr>
                    <td v-for="(label,index) in childData" v-if="!label.isVirtual" :key="index">{{ language == 'en' ? label.labelNameEn : label.labelName }}</td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.creditsEarned') }}</td>
                    <!-- <td v-for="label in childData">{{label.scores}}</td> -->
                    <td v-for="(label,index) in childData" v-if="!label.isVirtual" :key="index"><span :class="label.isRedColor && 'red'">{{ label.credit }}</span></td>
                    <td v-for="(label,index) in childData" v-if="label.isVirtual" :key="index"><span :class="label.isRedColor && 'red'">{{ label.credit }}</span></td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.subtotalCredits') }}</td>
                    <td v-for="(label,index) in parentData" :colspan="label.colspan" :key="index"><span :class="label.isPass == 1?'green':'red'">{{ label.credit }}</span></td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.totalCredits') }}</td>
                    <td :colspan="childData.length"><span :class="!scoreColor?'green':'red'">{{ scoreTotal }}{{ cultureSchemeTotalScoreRule }}</span></td>
                </tr>
            </tbody>
        </table>
        <p style="margin-bottom:0px;">{{ $t('culturePlan.scoreStatistics.subtotalCredits') }}{{ $t('culturePlan.scoreStatistics.totalCreditsFormat') }}</p>

        <p style="margin-top:5px;font-weight:bold">
            {{ $t('culturePlan.scoreStatistics.creditRequirement') }} <span :style="{color:isCompleted?'green':'red'}">{{ isCompleted?$t('culturePlan.scoreStatistics.completed'):$t('culturePlan.scoreStatistics.uncompleted') }}</span>：{{ $t('culturePlan.scoreStatistics.totalCreditsRequirement') }}<span class="red">{{ cultureSchemeTotalScoreRule }}</span>{{ $t('culturePlan.scoreStatistics.credit') }}，

            <template v-if="language=='en'">
                A number of <span class="red">{{ scoreLevelTotal }}</span> credits of degree courses must be an indispensable part for the total number of credits which, required by Study Plan, is <span class="red">{{ scorePlanTotal }}</span> credits, <span class="red">{{ scoreGetTotal }}</span> of which have been earned, including <span class="red">{{ scoreGetLevelTotall }}</span> credits of degree courses.
            </template>
            <template v-else>
                学位课<span class="red">{{ scoreLevelTotal }}</span>学分；个人培养计划总学分<span class="red">{{ scorePlanTotal }}</span>学分，已获<span class="red">{{ scoreGetTotal }}</span>学分，其中学位课<span class="red">{{ scoreGetLevelTotall }}</span>学分
            </template>

            <template v-for="(label,index) in tipsData">
                <span :key="index">，{{ language=='en'?label.labelNameEn:label.labelName }} <font class="red">{{ label.score }}</font>{{ $t('culturePlan.scoreStatistics.credit') }}</span>
            </template>。
        </p>
    </div>
    <div v-else>
        <table width="100%" class="personal" style="margin:15px 0">
            <tbody>
                <tr>
                    <td :colspan="secondLabels.length+1" style="font-size:20px;height:40px">{{ $t('culturePlan.scoreStatistics.aboutCreditsEarned') }}</td>
                </tr>
                <tr>
                    <td rowspan="2">{{ $t('culturePlan.scoreStatistics.courseType') }}</td>
                    <td
                        v-for="(label,index) in rootLabels"
                        :colspan="label.colspan"
                        :key="index"
                        :rowspan="label.rowspan"
                    >{{ language == 'en' ? label.labelNameEn : label.labelName }}</td>
                </tr>
                <tr>
                    <td v-for="label in secondLabels" v-if="!label.isVirtual" :key="index">{{ language == 'en' ? label.labelNameEn : label.labelName }}</td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.creditsEarned') }}</td>
                    <td v-for="(label,index) in secondLabels" :key="index"><b>0</b>/{{ label.score }}</td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.subtotalCredits') }}</td>
                    <td v-for="(label,index) in rootLabels" :colspan="label.colspan" :key="index"><b>0</b>/{{ label.score }}</td>
                </tr>
                <tr>
                    <td>{{ $t('culturePlan.scoreStatistics.totalCredits') }}</td>
                    <td :colspan="secondLabels.length"><b>0</b>/{{ scoreTotal }}</td>
                </tr>
            </tbody>
        </table>
    </div>
</template>
<script>
export default {
    name: 'ScoreStatistics',
    props: {
        labels: {
            type: String,
            default: null
        },
        courses: {
            type: String,
            default: null // 培养方案所有课程,不是培养计划课程
        },
        newLabel: {
            type: String,
            default: null
        },
        isGraduate: {
            type: Boolean,
            default: false
        },
        isWorked: {
            type: Boolean,
            default: false
        },
        cultureSchemeTotalScoreRule: {
            type: String,
            default: ''
        },
        isCompleted: {
            type: Boolean,
            default: true
        }
    },
    data() {
        return {
            columnTotal: 0, // 总列数
            rootLabels: [], // 根分类
            newLabels: [],
            secondLabels: [], // 二级分类
            scoreTotal: 0, // 分数总计
            parentData: [],
            childData: [],
            tipsData: [],
            scoreSchemeTotal: '',
            scorePlanTotal: '',
            scoreGetTotal: '',
            scoreLevelTotal: '',
            scoreGetLevelTotall: ''
        }
    },
    computed: {
        language() {
            return this.$store.getters.language
        }
    },
    watch: {
        newLabel(newData, oldData) {
            if (newData) {
                if (this.isWorked) {
                    this.newLabels = this.newLabel.filter(
                        child => child.labelId != 999999
                    )
                    this.getData()
                } else {
                    this.newLabels = this.newLabel
                    if (this.isGraduate) {
                        this.getData()
                    } else {
                        this.buildLabels()
                    }
                }
            }
        },
        deep: true
    },
    mounted() {
        if (this.isWorked) {
            this.newLabels = this.newLabel.filter(
                child => child.labelId != 999999
            )
            this.getData()
        } else {
            let flag = true
            if (this.newLabel.length > 0) {
                this.newLabel.forEach(d => {
                    if (d.labelName == '补修课') {
                        const data = d.credit.split('/')
                        const score1 = +data[0]
                        if (score1 == 0) {
                            flag = false
                        }
                    }
                })
                if (flag) {
                    this.newLabels = this.newLabel
                } else {
                    this.newLabels = this.newLabel.filter(
                        child => child.labelName != '补修课'
                    )
                }
            }
            if (this.isGraduate) {
                this.getData()
            } else {
                this.buildLabels()
            }
        }
    },
    methods: {
        getData() {
            this.parentData = this.newLabels.filter(
                child => child.parentId == null
            )
            this.childData = this.newLabels.filter(
                child => child.parentId != null
            )
            this.tipsData = this.newLabels.filter(
                child => child.parentId == 0
            )
            this.newLabels.forEach(d => {
                d.colspan = d.children ? d.children.length : 1
                d.rowspan = d.children ? 1 : 2
                if (d.parentId == 0) {
                    d.isVirtual = true
                    this.parentData.push(d)
                }
            })
            this.editData()
        },
        editData() {
            let score7 = 0
            let score8 = 0
            let score9 = 0
            let score10 = 0
            this.parentData.forEach(p => {
                let score1 = 0
                let score2 = 0
                let score3 = 0
                this.childData.forEach(d => {
                    if (p.labelId == d.parentId) {
                        const data = d.credit.split('/')
                        score1 += +data[0]
                        score2 += +data[1]
                        score3 += +data[2]
                        if (score1 >= score2) {
                            p.isPass = 1
                        } else {
                            p.isPass = 0
                        }
                        p.credit = score1 + '/' + score2 + '/' + score3
                        this.scoreLevelTotal = score3
                        this.scoreGetLevelTotall = score1
                    } else {
                        const xdata = d.credit.split('/')
                        const score4 = +xdata[0]
                        const score5 = +xdata[1]
                        const score6 = +xdata[2]
                        if (score4 < score6) {
                            d.isRedColor = true
                        } else {
                            d.isRedColor = false
                        }
                        d.credit = score4 + '/' + score5 + '/' + score6
                        d.scores = score4 + '/' + score5
                        if (d.labelName == '补修课') {
                            score10 = +xdata[1]
                        }
                    }
                })
                if (p.isPass != 1) {
                    this.scoreColor = true
                }
                const ydata = p.credit.split('/')
                score7 += +ydata[0]
                score8 += +ydata[1]
                score9 += +ydata[2]
            })
            if (this.scoreLevelTotal == '') {
                this.scoreLevelTotal = 0
            }
            if (this.scoreGetLevelTotall == '') {
                this.scoreGetLevelTotall = 0
            }
            if (this.tipsData.length > 0) {
                this.tipsData = this.tipsData.filter(t => !t.labelName.includes('补修课'))
                this.tipsData.forEach(d => {
                    const zdata = d.credit.split('/')
                    const score = +zdata[0]
                    d.score = score
                })
            }
            this.scoreTotal = score7 + '/' + score8 + '/'
            this.scoreSchemeTotal = score9
            // this.scorePlanTotal = score8;
            this.scorePlanTotal = score8 - score10
            this.scoreGetTotal = score7
            if (this.cultureSchemeTotalScoreRule == 0) {
                this.cultureSchemeTotalScoreRule = score9
            }
        },
        buildLabels() {
            const leafLabels = this.initAndGetLeafLabels()
            const finishedLabels = []
            leafLabels.forEach(label => {
                this.calcScore(label.parentId, finishedLabels)
            })
            this.calcRowCol()
        },
        initAndGetLeafLabels() {
            this.labels.forEach(label => {
                label.score = 0
                if (!this.labels.some(child => child.parentId == label.labelId)) {
                    label.isLeaf = true
                }
            })
            const leafLabels = this.labels.filter(label => label.isLeaf)
            leafLabels.forEach(label => {
                this.courses.forEach(c => {
                    if (c.labelId == label.labelId) {
                        label.score += c.credits
                        this.scoreTotal += c.credits
                    }
                })
            })
            return leafLabels
        },
        // 递归计算各分类学分
        calcScore(labelId, finishedLabels) {
            if (!finishedLabels.includes(labelId)) {
                const currentLabel = this.labels.find(label => label.labelId == labelId)
                if (currentLabel) {
                    currentLabel.score = _.sumBy(
                        this.labels.filter(label => label.parentId == labelId),
                        label => label.score
                    )
                    finishedLabels.push(labelId)
                    if (currentLabel.parentId != '0') {
                        this.calcScore(currentLabel.parentId, finishedLabels)
                    }
                }
            }
        },
        calcRowCol() {
            this.labels
                .filter(label => label.parentId == '0')
                .forEach(label => {
                    const childrenLabels = this.labels.filter(
                        child => child.parentId == label.labelId
                    )
                    label.children = childrenLabels.length
                    this.rootLabels.push(label)
                    this.secondLabels = this.secondLabels.concat(childrenLabels)
                    if (childrenLabels.length == 0) {
                        const virtualLabel = _.cloneDeep(label)
                        virtualLabel.isVirtual = true
                        this.secondLabels.push(virtualLabel)
                    }
                    label.colspan = childrenLabels.length > 0 ? childrenLabels.length : 1
                    label.rowspan = childrenLabels.length == 0 ? 2 : 1
                })
        }
    }
}
</script>
<style lang="scss" scoped>
.personal {
	border-collapse: collapse;
	border: 1px solid #000000;
	td,
	th {
		text-align: center;
		line-height: 30px;
		border-collapse: collapse;
		border: 1px solid #a5a5a5;
		background: #fff;
		color: #606266;
	}
}
.red{
	color: #ff0000;
}
.green{
	color: #009900;
}
</style>
